def F(x):
    return x**2  # 定义力函数

def work_done(a, b, n):
    """
    计算变力做的功
    """
    dx = (b - a) / n  # 位移小段长度
    total_work = 0.0
    
    for i in range(n):
        xi = a + i * dx  # 取点 ξ_i
        total_work += F(xi) * dx  # 求和 F(ξ_i) Δx_i
    
    return total_work

# 计算变力做功
a, b = 0, 1
n = 1000
approx_work = work_done(a, b, n)

print(f"变力做功近似值 (n={n}): {approx_work:.6f}")
print(f"精确值: {1/3:.6f}")
print(f"误差: {abs(approx_work - 1/3):.6f}")